package cn.crex1.xiaomi.controller;

import java.io.IOException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.crex1.xiaomi.service.CategorySService;
import cn.crex1.xiaomi.service.impl.CategorySServiceImpl;
import cn.crex1.xiaomi.vo.ProductVO;

@WebServlet("/category/*")
public class CategorySController extends HttpServlet {
	private CategorySService categorySService=new CategorySServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
 
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String q[] = req.getRequestURI().split("/");
		for (String i:q) {
			System.out.println(i);
		}
		System.out.println(q[3]);
		if (q[3].equals("pageAndTotalpages")) {
			pageAndTotalpages(req,resp);
		}
		pageAndTotalpages(req, resp);
	
	}
	private void pageAndTotalpages(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, JsonGenerationException, JsonMappingException {
		resp.setContentType("text/html;charset=utf-8");
		
		String names=req.getParameter("page");
		
		ProductVO productVO=new ProductVO();
		
		productVO.setCategory_id(Integer.parseInt(names));
		List<ProductVO> oProducts=categorySService.selectOProductByOCategoryName(productVO);
			int pageCounts=oProducts.size();//获取查询到的数据总数
			int counts=pageCounts;
			int	pages=((counts-1)/10)+1;//判断多少页
			System.out.println("总页数"+pages);
			Map<String,List<Object>> map = new HashMap<String, List<Object>>();
			List<Object> list=new ArrayList<Object>();
			List<Object> list1=new ArrayList<Object>();
			//以下给查询出来第一页中的数据
				for(int i=0;i<pageCounts;i++) {
					if(i<10) {
					List<Object> list11=new ArrayList<Object>();
					String name=oProducts.get(i).getName();
					double price=oProducts.get(i).getShow_price();
					String introduce=oProducts.get(i).getIntroduce();
					String img=oProducts.get(i).getPic_img();
					String[] listimg=img.split("/");
					String subimg=listimg[0]+"/"+listimg[2]+"/"+listimg[3];
					
					long id=oProducts.get(i).getProduct_number();
					list11.add(name);
					list11.add(price);
					list11.add(introduce);
					list11.add(subimg);
					list11.add(id);
					list1.add(list11);
					System.out.println(list1);
					}
				}
				list.add(pages);
				map.put("state", list);
				map.put("state1", list1);
				
			/*	list.add(totalpages);
				map.put("state", list);*/
		ObjectMapper om = new ObjectMapper();
		om.writeValue(resp.getWriter(), map);

	}
}
